
#include <swegl/data/Tore.h>
#include <swegl/data/Texture.h>
#include <swegl/projection/Vec3f.h>
#include <swegl/projection/Vec2f.h>

namespace swegl
{

	Tore::Tore(unsigned int precision, Texture *t)
	{
		Mesh::Mesh();

		this->m_texture = t;
		this->m_vertexbuffer.reserve(precision*precision);

		float angle = (2*3.141592653589f) / precision;
		Matrix4x4 big, small;
		big.SetIdentity();
		small.SetIdentity();

		big.Translate(2.0f, 0.0f, 0.0f);
		small.Translate(0.8f, 0.0f, 0.0f);

		for (unsigned int bg=0 ; bg<precision ; bg++)
		{
			for (unsigned int sm=0 ; sm<precision ; sm++)
			{
				Vec3f v;
				v = small * v;
				v = big * v;
				m_vertexbuffer.emplace_back(std::move<>(v));
				small.RotateZ(angle);
			}
			big.RotateY(angle);
		}

		for (unsigned int bg=0 ; bg<precision ; bg++)
		{
			std::vector<unsigned int> ib;
			ib.resize(precision*2 + 2, 0);
			std::vector<Vec2f> tb;
			tb.resize(3*precision*2, swegl::Vec2f(0,0));

			ib[0] = bg*precision;
			ib[1] = ((bg+1)%precision)*precision;
			for (unsigned int i=1 ; i<=precision ; i++)
			{
				ib[i*2]   = bg*precision + (i%precision);
				ib[i*2+1] = ((bg+1)%precision)*precision + (i%precision);

				tb[(i-1)*6].x = (t->m_mipmaps[0].m_width*1.0f / precision) * bg;
				tb[(i-1)*6].y = (t->m_mipmaps[0].m_height*1.0f / precision) * (i-1);
				tb[(i-1)*6+1].x = (t->m_mipmaps[0].m_width*1.0f / precision) * (bg+1);
				tb[(i-1)*6+1].y = (t->m_mipmaps[0].m_height*1.0f / precision) * (i-1);
				tb[(i-1)*6+2].x = (t->m_mipmaps[0].m_width*1.0f / precision) * bg;
				tb[(i-1)*6+2].y = (t->m_mipmaps[0].m_height*1.0f / precision) * i;
			
				tb[(i-1)*6+3].x = (t->m_mipmaps[0].m_width*1.0f / precision) * (bg+1);
				tb[(i-1)*6+3].y = (t->m_mipmaps[0].m_height*1.0f / precision) * (i-1);
				tb[(i-1)*6+4].x = (t->m_mipmaps[0].m_width*1.0f / precision) * bg;
				tb[(i-1)*6+4].y = (t->m_mipmaps[0].m_height*1.0f / precision) * i;
				tb[(i-1)*6+5].x = (t->m_mipmaps[0].m_width*1.0f / precision) * (bg+1);
				tb[(i-1)*6+5].y = (t->m_mipmaps[0].m_height*1.0f / precision) * i;
			
			}
			this->AddStrip(std::move<>(ib), std::move<>(tb));
		}
	}

}
